define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'content/goods' + location.search,
                    add_url: 'content/add',
                    edit_url: 'content/edit',
                    del_url: 'content/del',
                    multi_url: 'content/multi',
                    import_url: 'content/import',
                    table: 'content',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'desc',
                searchFormVisible: false,
                commonSearch: true,
                search: true,
                showRefresh: true,
                showToggle: true,
                showColumns: true,
                showExport: true,
                exportDataType: "basic",
                exportTypes: ['json', 'xml', 'csv', 'txt', 'sql', 'excel'],
                pageSize: 10,
                pageList: [10, 25, 50, 100],
                sidePagination: 'server',
                toolbar: '#toolbar',
                columns: [
                    [
                        {checkbox: true, width: 50, align: 'center'},
                        {field: 'id', title: 'ID', width: 60, align: 'center', sortable: true,
                         formatter: function(value, row, index) {
                             return '<span class="text-muted">' + value + '</span>';
                         }
                        },
                        {field: 'name', title: '商品类型名称', width: 250, align: 'left', sortable: true,
                         formatter: function(value, row, index) {
                             return '<strong class="text-primary">' + value + '</strong>';
                         }
                        },
                        {field: 'image', title: '图片', width: 100, align: 'center', 
                         events: Table.api.events.image, 
                         formatter: function(value, row, index) {
                             if (!value) {
                                 return '<span class="text-muted">无图片</span>';
                             }
                             return '<img src="' + value + '" class="img-thumbnail" style="width:60px;height:60px;object-fit:cover;" onclick="Fast.api.open(\'' + value + '\', \'查看图片\', {area: [\'80%\', \'80%\']})">';
                         }
                        },
                        {field: 'description', title: '描述信息', width: 300, align: 'left',
                         formatter: function(value, row, index) {
                             if (!value) return '<span class="text-muted">暂无描述</span>';
                             var shortText = value.length > 40 ? value.substring(0, 40) + '...' : value;
                             return '<span class="text-info" title="' + value + '">' + shortText + '</span>';
                         }
                        },
                        {field: 'status', title: '状态', width: 80, align: 'center', sortable: true,
                         formatter: function(value, row, index) {
                             var statusConfig = {
                                 'normal': {color: 'success', text: '正常', icon: 'fa-check-circle'},
                                 'hidden': {color: 'danger', text: '隐藏', icon: 'fa-times-circle'}
                             };
                             var config = statusConfig[value] || {color: 'default', text: value, icon: 'fa-question-circle'};
                             return '<span class="label label-' + config.color + '"><i class="fa ' + config.icon + '"></i> ' + config.text + '</span>';
                         }
                        },
                        {field: 'createtime', title: '创建时间', width: 140, align: 'center', sortable: true,
                         formatter: function(value, row, index) {
                             if (!value) return '-';
                             var date = new Date(value * 1000);
                             return '<small class="text-muted">' + date.toLocaleString('zh-CN') + '</small>';
                         }
                        },
                        {field: 'operate', title: '操作', width: 120, align: 'center', 
                         table: table, 
                         events: Table.api.events.operate, 
                         formatter: function(value, row, index) {
                             var html = [];
                             html.push('<div class="btn-group btn-group-xs">');
                             if (table.data('operate-edit')) {
                                 html.push('<a href="javascript:;" class="btn btn-xs btn-success btn-editone" title="编辑"><i class="fa fa-pencil"></i></a>');
                             }
                             if (table.data('operate-del')) {
                                 html.push('<a href="javascript:;" class="btn btn-xs btn-danger btn-delone" title="删除"><i class="fa fa-trash"></i></a>');
                             }
                             html.push('</div>');
                             return html.join('');
                         }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            
            // 绑定导入导出事件
            $(document).on('click', '.btn-import', function() {
                Fast.api.open('content/import', '导入数据', {
                    area: ['800px', '600px'],
                    callback: function(data) {
                        table.bootstrapTable('refresh');
                    }
                });
            });
            
            $(document).on('click', '.btn-export', function() {
                Fast.api.open('content/export', '导出数据', {
                    area: ['600px', '400px']
                });
            });
        },
        goods: function () {
            Controller.index();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});